package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.common.RequirePermission;
import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.domain.query.QueryObject;
import cn.wolfcode.rbac.domain.vo.PageResult;
import cn.wolfcode.rbac.domain.vo.R;
import cn.wolfcode.rbac.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@Controller
@RequestMapping("/api/permission")
@CrossOrigin(allowCredentials = "true")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/listAll")
    @ResponseBody
    @RequirePermission({"权限列表","permission:listAll"})
    public R listAll() {
        Set<Permission> permissions = permissionService.listAll();
        return R.ok(permissions);
    }

    @GetMapping("/list")
    @ResponseBody
    @RequirePermission({"权限分页列表","permission:list"})
    public R list(QueryObject qo) {
        PageResult<Permission> pageResult = permissionService.queryForList(qo);
        return R.ok(pageResult);
    }

    @PostMapping("/load")
    @ResponseBody
    @RequirePermission({"加载权限","permission:load"})
    public R load() {
        permissionService.load();
        return R.ok("加载权限数据成功");
    }

    @GetMapping("/queryPermission/{id}")
    @ResponseBody
    @RequirePermission({"查询权限","department:queryPermission"})
    public R queryPermission(@PathVariable Long id) {
        Permission permission = permissionService.queryPermission(id);
        return R.ok(permission);
    }
}
